package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.salelogistics.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.domain.BasMaterialType;
import com.salelogistics.basic.service.IBasMaterialTypeService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;

/**
 * 材料类型Controller
 * 
 * @author ruoyi
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/type")
public class BasMaterialTypeController extends BaseController
{
    @Autowired
    private IBasMaterialTypeService basMaterialTypeService;

    /**
     * 查询材料类型列表
     */
    @RequiresPermissions("basic:type:list")
    @GetMapping("/list")
    public AjaxResult list(BasMaterialType basMaterialType)
    {
        List<BasMaterialType> list = basMaterialTypeService.selectBasMaterialTypeList(basMaterialType);
        return success(list);
    }

    /**
     * 导出材料类型列表
     */
    @RequiresPermissions("basic:type:export")
    @Log(title = "材料类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasMaterialType basMaterialType)
    {
        List<BasMaterialType> list = basMaterialTypeService.selectBasMaterialTypeList(basMaterialType);
        ExcelUtil<BasMaterialType> util = new ExcelUtil<BasMaterialType>(BasMaterialType.class);
        util.exportExcel(response, list, "材料类型数据");
    }

    /**
     * 获取材料类型详细信息
     */
    @RequiresPermissions("basic:type:query")
    @GetMapping(value = "/{materialTypeId}")
    public AjaxResult getInfo(@PathVariable("materialTypeId") Long materialTypeId)
    {
        return success(basMaterialTypeService.selectBasMaterialTypeByMaterialTypeId(materialTypeId));
    }

    /**
     * 新增材料类型
     */
    @RequiresPermissions("basic:type:add")
    @Log(title = "材料类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasMaterialType basMaterialType)
    {
        basMaterialType.setCreateBy(SecurityUtils.getUsername());
        basMaterialType.setEditedBy(SecurityUtils.getUsername());
        return toAjax(basMaterialTypeService.insertBasMaterialType(basMaterialType));
    }

    /**
     * 修改材料类型
     */
    @RequiresPermissions("basic:type:edit")
    @Log(title = "材料类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasMaterialType basMaterialType)
    {
        // 创建一个Date对象，表示当前时间
        Date date = new Date();
        // 创建一个SimpleDateFormat对象，指定时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
        String currentTime = sdf.format(date);
        basMaterialType.setEditedBy(SecurityUtils.getUsername());
        basMaterialType.setEditedTime(currentTime);
        return toAjax(basMaterialTypeService.updateBasMaterialType(basMaterialType));
    }

    /**
     * 删除材料类型
     */
    @RequiresPermissions("basic:type:remove")
    @Log(title = "材料类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialTypeIds}")
    public AjaxResult remove(@PathVariable Long[] materialTypeIds)
    {
        return toAjax(basMaterialTypeService.deleteBasMaterialTypeByMaterialTypeIds(materialTypeIds));
    }
}
